package com.bocai.eelly.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.common.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HistoryFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_history_cg)
    TextView tvHistoryCg;
    @Bind(R.id.tv_history_gy)
    TextView tvHistoryGy;
    @Bind(R.id.vp_history)
    ViewPager vpHistory;
    private View view;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_history, null);
        initView();
        return view;

    }

    private void initView() {
        ButterKnife.bind(this, view);
        tvTitle.setText(R.string.str_title_history);
        vpHistory.setCurrentItem(0);
        vpHistory.setAdapter(new Adapter(getActivity().getSupportFragmentManager()));
        initEvent();
//        getHistoryList();
    }

    private void initEvent() {
        tvHistoryCg.setOnClickListener(this);
        tvHistoryGy.setOnClickListener(this);
        vpHistory.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==0){
                    resetTvBag();
                    tvHistoryCg.setBackground(getResources().getDrawable(R.drawable.bg_getcode_button));

                    tvHistoryCg.setTextColor(getResources().getColor(R.color.cl_white));
                }else if(position==1){
                    resetTvBag();
                    tvHistoryGy.setBackground(getResources().getDrawable(R.drawable.bg_getcode_button));
                    tvHistoryGy.setTextColor(getResources().getColor(R.color.cl_white));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void resetTvBag() {
        tvHistoryCg.setBackgroundColor(getResources().getColor(R.color.cl_white));
        tvHistoryGy.setBackgroundColor(getResources().getColor(R.color.cl_white));
        tvHistoryCg.setTextColor(getResources().getColor(R.color.cl_black));
        tvHistoryGy.setTextColor(getResources().getColor(R.color.cl_black));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_history_cg:
                vpHistory.setCurrentItem(0);
                break;
            case R.id.tv_history_gy:
                vpHistory.setCurrentItem(1);
                break;
        }
    }

    class Adapter extends FragmentPagerAdapter
    {
        private final Fragment[] mFragments = new Fragment[2];

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {

            Fragment fragment = mFragments[position];
            if (fragment == null)
            {
                if(position==0){

                    fragment = new HistoryCGFragment();
                    mFragments[position] = fragment;
                }else{
                    fragment = new HistoryGYFragment();
                    mFragments[position] = fragment;
                }
            }
            return fragment;
        }

        @Override
        public int getCount()
        {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position)
        {

            return "";
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
